<?php

namespace app\admin\controller\ai;

use app\admin\logic\AiLogic;
use app\admin\validate\OrderValidate;
use app\common\controller\AdminBase;
use app\common\helper\AliOss;
use app\common\model\AiImage;
use app\common\model\AiOrder;
use app\common\model\OrderOrder;
use app\common\model\OrderOther;
use think\db\Query;
use think\Exception;
use think\exception\ValidateException;
use think\facade\Db;
use think\facade\View;
use think\helper\Str;

class Order extends AdminBase
{
    public function initialize()
    {
        parent::initialize(); // TODO: Change the autogenerated stub
        $this->model = new \app\common\model\AiOrder();
        $this->doValidate = OrderValidate::class;
    }

    public function doCreate()
    {
        AiLogic::doCreate();
    }

    public function doPull()
    {
        AiLogic::doPull();
    }

    public function doSee()
    {
        AiLogic::doSee();
    }

    public function del()
    {
        $paths = AiImage::where("order_id",'in',input('id'))
            ->where('store_type',"<>","public")
            ->column('path');
        AiImage::where("order_id",'in',input('id'))
            ->where('store_type',"<>","public")
            ->delete();
        $path = [];
        foreach ($paths as $p) {
            if ($p) {
                $path[] = ltrim($p,"/");
            }
        }
        if (!empty($path)) {
            delFile($path);
            (new AliOss())->del($paths);
        }
        parent::del(); // TODO: Change the autogenerated stub
    }

    public function setStatus()
    {
        $data = input();
        if ($this->request->isPost()) {
            if (!empty($data['id'])) {
                $u = $this->model->where('id',$data['id'])->find();
                if (!$u->sign_info) {
                    $this->error('请先输入签名');
                }
                $u->save(['status'=>$data['status']]);
                $this->success();
            }
        }
        return View::fetch();
    }
    
    public function printSearch()
    {
        $result = [];
        $data = input();
        $obj = new OrderOrder();
        if (isset($data['time_range']) && !empty($data['time_range'])) {
            if ($data['type'] == 1) {
                $result = $obj->whereBetweenTime('create_time',$data['time_range'][0],$data['time_range'][1] . "23:59:59")
                    ->with($this->with)->where($this->where)
                    ->where('status',1)
                    ->select();
            }
            if ($data['type'] == 2) {
                $result = $obj->whereBetweenTime('liu_time',$data['time_range'][0],$data['time_range'][1] . "23:59:59")
                    ->with($this->with)->where($this->where)
                    ->where('status',1)
                    ->select();
            }
            if ($data['type'] == 3) {
                $result = $obj
                    ->with($this->with)
                    ->where('status',1)
                    ->where(function (Query $query) use ($data) {
                        $query->whereBetweenTime('liu_time',$data['time_range'][0],$data['time_range'][1] . "23:59:59")
                            ->whereBetweenTime('create_time',$data['time_range'][0],$data['time_range'][1] . "23:59:59","OR");
                    })->where($this->where)
                    ->select();
            }
            $this->success($result);
        }
    }

    // 退款
    public function orderReturned()
    {
        \app\common\model\Order::where("id",$this->data['id'])
            ->save([
                "no_why" => $this->data['no_why'],
                "return_money" => $this->data['return_money'],
                "status" => $this->data['status']
            ]);
        $this->success();
    }
}